<template>
  <div>
    <div class="breadcrumb_wrap">
      <span class="current">当前位置：</span>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/personalcenter' }">个人中心</el-breadcrumb-item>
        <el-breadcrumb-item>资料中心</el-breadcrumb-item>
        <el-breadcrumb-item>密码管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-button
        style="position: absolute; right: 10px; top: 5px"
        type="primary"
        plain
        size="small"
        @click="toDoctorCertify"
      >医生认证</el-button>
    </div>

    <div class="pwd_card">
      <div class="pwd_form_wrap">
        <el-form
          :model="pwdForm"
          label-width="80px"
          label-position="right"
          :rules="rules"
        >
          <el-form-item label="当前密码" prop="oldPwd">
            <el-input
              v-model="pwdForm.oldPwd"
              placeholder="请输入当前密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPwd">
            <div class="form_line">
              <el-tooltip
                content="请输入4-20位大小写字母、数字或下划线，不能为纯数字"
                placement="right"
              >
                <el-input
                  v-model="pwdForm.newPwd"
                  placeholder="请输入新密码"
                  show-password
                ></el-input>
              </el-tooltip>
            </div>
          </el-form-item>
          <el-form-item label="确认密码" prop="newRePwd" required>
            <el-input
              v-model="pwdForm.newRePwd"
              placeholder="请输入确认密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSubmit">保存</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>


<script>
import verifyCode from "@/components/verifyCode.vue";
export default {
  name: "personData",
  data() {
    const checkPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.pwdForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userInfo: {
        gender: 1,
      },
      tabsActive: "base",
      emailForm: {
        email: "",
        verifyCode: "",
      },
      identifyCodes: "1234567890abcdef",
      identifyCode: "",
      pwdForm: {
        oldPwd: "",
        newPwd: "",
        newRePwd: "",
      },
      rules: {
        oldPwd: [
          {
            required: true,
            message: "请输入当前密码",
            trigger: "blur",
          },
        ],
        newPwd: [
          {
            required: true,
            message: "请输入新密码",
            trigger: "blur",
          },
          {
            pattern: /\D+/g,
            message: "密码不能是纯数字",
            trigger: "blur",
          },
          {
            pattern: /^[0-9a-zA-Z]{4,20}$/,
            message: "密码必须为4到20位的字母或者数字",
            trigger: "blur",
          },
        ],
        newRePwd: [
          {
            validator: checkPwd,
            trigger: "blur",
          },
        ],
      },
    };
  },

  watch: {
    $route: {
      handler() {
        this.tabsActive = this.$route.query.tab;
        console.log("tabsActive::", this.tabsActive);
      },
      deep: true,
    },
  },
  components: {
    verifyCode,
  },
  methods: {
    // 验证码处理
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    // 刷新验证码
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
    },
    handleSelect(key, keyPath) {
      //路由
      this.$router.push({ name: key, params: { activityId: "0" } });
    },
    //跳转到医生认证
    toDoctorCertify() {
      this.$router.push("/doctorCertify");
    },
	handleSubmit(){
		this.updatePassword(this.pwdForm.oldPwd,this.pwdForm.newPwd);
	},
	async updatePassword(oldPwd,newPwd){
		const res = await this.$api.reqUpdatePwd(oldPwd,newPwd);
		if(res.code==200){
			// this.$cookies.remove("token");
			// this.$store.commit("user/SET_LOGIN", false);
			this.$message({
			  type: "success",
			  message: "修改成功",
			});

		}else{
      this.$message({
        type: "fail",
        message: "账号或密码不正确",
      });
    }
	}
  },
  mounted() {
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
  },
};
</script>

<style lang="scss" scoped>
.breadcrumb_wrap {
  width: 100%;
  height: 40px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding-left: 10px;
  box-sizing: border-box;
  .current {
    font-size: 14px;
  }
}

.pwd_card {
  background-color: #fff;
  margin-top: 20px;
  .pwd_form_wrap {
    width: 500px;
    padding: 30px;
    .form_line {
      display: flex;
      align-items: center;
    }
  }
}
</style>
